/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dabarobjects.mobile.database;

import com.dabarobjects.mobile.database.types.MadexObjectNotationTypeParser;
import java.util.Hashtable;

/**
 *
 * @author DabarObjects Solutions Limited (www.dabarobjects.com)
 */
public class DBMSObjectParser {

    private final static DBMSObjectParser INSTANCE = new DBMSObjectParser();

    private DBMSObjectParser() {
        registeredParsers = new Hashtable();
    }

    public static DBMSObjectParser get() {
        return INSTANCE;
    }
    private Hashtable registeredParsers;

    public void registerType(MadexObjectNotationTypeParser typeParser) {
        registeredParsers.put(typeParser.getObjectTypeKey(), typeParser);
    }

    public MadexObjectNotationTypeParser getParser(String typeKey) {
        return (MadexObjectNotationTypeParser) registeredParsers.get(typeKey);
    }

    public String notate(String typeKey, Object obj) {
        MadexObjectNotationTypeParser parser = (MadexObjectNotationTypeParser) registeredParsers.get(typeKey);
        return parser.notateObject(obj);
    }

    public Object parseObject(String typeKey, String objNotation) {
        if(typeKey == null  || typeKey.equalsIgnoreCase(""))
            typeKey = "st";
        if (objNotation.charAt(0) == '{') {
            objNotation = objNotation.substring(1);
        }
        if (objNotation.charAt(objNotation.length() - 1) == '}') {
            objNotation = objNotation.substring(0, objNotation.length() - 1);
        }

        MadexObjectNotationTypeParser parser = (MadexObjectNotationTypeParser) registeredParsers.get(typeKey);
        return parser.parseObject(objNotation);
    }

    public Hashtable parse(String dump) {
        //System.out.println("DUMP: " + dump);
        int len = dump.length();

        Hashtable htable = new Hashtable();
        boolean startAppend = false;
        boolean startKeyAppend = true;

        int j = 0;
        StringBuffer valueBuffer = new StringBuffer();
        StringBuffer keyBuffer = new StringBuffer();
        int pointer = 0;
        for (int i = 0; i < len; i++) {
            String it = dump.substring(i, i + 1);


            if (it.equalsIgnoreCase(":")) {
                pointer++;
                valueBuffer = new StringBuffer();
                startAppend = true;
                startKeyAppend = false;

            }
            if (it.equalsIgnoreCase(";")) {


                startAppend = false;
                startKeyAppend = true;
                htable.put(keyBuffer.toString(), valueBuffer.toString());
                //System.out.println(valueBuffer.toString());


                keyBuffer = new StringBuffer();
            }
            if (startAppend) {
                if (!it.equalsIgnoreCase(":")) {
                    valueBuffer.append(it);
                }
            }
            if (startKeyAppend) {

                if (!it.equalsIgnoreCase(";")) {
                    keyBuffer.append(it);
                }
            }

        }
        //htable.put(keyBuffer.toString(), valueBuffer.toString());

        return htable;


    }
}
